/**
 * Created by xiaoz on 16/8/13.
 */
define(["utils","base"],function(utils,baseClass){
    var Component = function(config){
        var _this = this;
        Component.baseConstructor.call(this,config);
        _this.config.style = _this.config.style||{};
        var className = "yy-image displayflex yy-jc-center yy-ai-center";
        this.src = config.src || config.defaultSrc;
        var width = this.config.style.width;
        try{
            width = parseInt(width);
        }catch(e){
            width = null;
        }
        var height = this.config.style.height;
        try{
            height = parseInt(height);
        }catch(e){
            height = null;
        }
        this.img = new Image();
        var preErrorImagesrc = "";
        this.img.src = this.src;
        _this.img.style["display"] = "none";

        this.img.onload = function(){
          var style = {"display":"block"};
            if(height&&width){
                var size = utils.getPicSize(width,height,_this.img.width,_this.img.height);
                style.width = size.w;
                style.height = size.h;
            }else{
              style.width = "100%";
              style.height = "100%";
            }
            if(_this.config.style.borderRadius){
              style.borderRadius = _this.config.style.borderRadius;
            }
            utils.css($(_this.img),style);
        }
        this.img.onerror = function(){
            if(config.defaultSrc){
                if(preErrorImagesrc===config.defaultSrc){
                    _this.img.classList.add("displaynone");
                    return;
                }
                preErrorImagesrc = config.defaultSrc;
                _this.img.src = config.defaultSrc;
            }
        }
        _this.$el.append(_this.img);
        this.$el.addClass(className);
    }

    utils.extends(Component,baseClass);

    return Component;
});
